import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from urllib.parse import quote

chrome_path = r"D:\selenium\chrome-win64\chrome.exe"
options = webdriver.ChromeOptions()
options.binary_location = chrome_path

service = ChromeService(executable_path="./chromedriver.exe")
driver = webdriver.Chrome(service=service, options=options)

# 直接构造搜索URL，完全避免页面交互问题
search_word = "Selenium自动化测试"
# 对搜索词进行URL编码
encoded_word = quote(search_word)
search_url = f"https://www.baidu.com/s?wd={encoded_word}"

print(f"直接访问搜索URL: {search_url}")
driver.get(search_url)

print("页面标题:", driver.title)
print("当前URL:", driver.current_url)

time.sleep(3)
driver.quit()